package com.pur.zsms.financing.service.impl;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.pur.zsms.check.vo.CheckVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.FileUtill;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.common.util.pub.FDate;
import com.pur.zsms.cost.dao.ICostDao;
import com.pur.zsms.cost.po.CostPo;
import com.pur.zsms.cost.po.CostSubPo;
import com.pur.zsms.cost.service.ICostService;
import com.pur.zsms.cost.vo.CostVo;
import com.pur.zsms.financing.dao.IFinancingDao;
import com.pur.zsms.financing.service.IFinancingService;
import com.pur.zsms.financing.vo.FinancingDetailVo;
import com.pur.zsms.financing.vo.FinancingManagerVo;
import com.pur.zsms.financing.vo.FinancingVo;
import com.pur.zsms.financing.vo.PictureVo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.web.OrderCustControllor;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.timecontrol.dao.ITimeControlDao;
import com.pur.zsms.timecontrol.po.TimeControlPo;
import com.pur.zsms.timecontrol.service.ITimeControlService;
import com.pur.zsms.turnover.dao.ITurnoverDao;
import com.pur.zsms.turnover.po.TurnoverPo;
import com.pur.zsms.turnover.po.TurnoverSubPo;
import com.pur.zsms.turnover.service.ITurnoverService;
import com.pur.zsms.turnover.vo.TurnoverVo;

@Service("financingService")
public class FinancingServiceImpl implements IFinancingService {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(OrderCustControllor.class);
	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "turnoverDao")
	private ITurnoverDao turnoverDao;

	@Resource(name = "costDao")
	private ICostDao costDao;

	@Resource(name = "financingDao")
	private IFinancingDao financingDao;

	@Override
	public PaginationSupport listFinancingList(PageController pageController, FinancingVo tFinancingVo) throws GenericException {
		return turnoverDao.listFinancingVoList(pageController, tFinancingVo);
	}

	public PaginationSupport auditFinancingList(PageController pageController, FinancingVo tFinancingVo) throws GenericException {
		return turnoverDao.auditFinancingList(pageController, tFinancingVo);

	}

	public TurnoverPo getTurnoverPo(FinancingVo financingVo) throws GenericException {
		// 状态校验
		TurnoverVo checkVo = new TurnoverVo();
		checkVo.setCustId(financingVo.getCustId());
		checkVo.setTurnoverDate(financingVo.getDate());
		List<TurnoverPo> listPo = turnoverDao.getTurnoverPo(checkVo);
		if (listPo != null && listPo.size() > 0) {
			msg = "msg001";
			return null;
		}
		/*
		 * // 校验是否审核完毕 TurnoverVo turnoverVo2 = new TurnoverVo();
		 * turnoverVo2.setTurnoverState(1);
		 * turnoverVo2.setTurnoverMonth(turnoverVo
		 * .getTurnoverDate().substring(0, 7)); List<TurnoverPo> listPo2 =
		 * turnoverDao.getTurnoverPo(turnoverVo2); if (listPo2 != null &&
		 * listPo2.size() > 0) { msg = "msg002"; return null; }
		 */
		TurnoverPo po = new TurnoverPo();
		BeanCopier copier = BeanCopier.create(FinancingVo.class, TurnoverPo.class, false);
		copier.copy(financingVo, po, null);
		// turnoverDao.addTurnoverPo(po);
		return po;
	}

	public CostPo getCostPo(FinancingVo financingVo) throws GenericException {
		// 状态校验
		CostVo checkVo = new CostVo();
		checkVo.setCustId(financingVo.getCustId());
		checkVo.setCostDate(financingVo.getDate());
		List<CostPo> listPo = costDao.getCostPo(checkVo);
		if (listPo != null && listPo.size() > 0) {
			msg = "msg002";
			return null;
		}
		/*
		 * // 校验是否审核完毕 TurnoverVo turnoverVo2 = new TurnoverVo();
		 * turnoverVo2.setTurnoverState(1);
		 * turnoverVo2.setTurnoverMonth(turnoverVo
		 * .getTurnoverDate().substring(0, 7)); List<TurnoverPo> listPo2 =
		 * turnoverDao.getTurnoverPo(turnoverVo2); if (listPo2 != null &&
		 * listPo2.size() > 0) { msg = "msg002"; return null; }
		 */
		CostPo po = new CostPo();
		BeanCopier copier = BeanCopier.create(FinancingVo.class, CostPo.class, false);
		copier.copy(financingVo, po, null);
		// turnoverDao.addTurnoverPo(po);
		return po;
	}

	public CostSubPo setPictureCost(PictureVo pictureVo, CostSubPo tCostSubPo, String filePath) throws GenericException, SecurityException,
			NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IOException {

		// 根据CostSubPo属性获得应该取得的图片属性
		String pictureNameCostSubPo = "picture" + tCostSubPo.getCostSubId();

		Field[] field = pictureVo.getClass().getDeclaredFields();
		for (int j = 0; j < field.length; j++) { // 遍历所有属性
			String name = field[j].getName(); // 获取属性的名字
			if (pictureNameCostSubPo.equals(name)) {
				name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
				String type = field[j].getGenericType().toString(); // 获取属性的类型
				if (type.equals("interface org.springframework.web.multipart.MultipartFile")) { // 如果type是类类型，则前面包含"class "，后面跟类名
					Method m = pictureVo.getClass().getMethod("get" + name);
					MultipartFile file = (MultipartFile) m.invoke(pictureVo); // 调用getter方法获取属性值
					if (file != null) {
						String filename1 = tCostSubPo.getCostId() + "-" + tCostSubPo.getCostSubId() + "-" + file.getOriginalFilename();
						FileUtill.saveFileFromInputStreamSeparator(file.getInputStream(), filePath, filename1);
						tCostSubPo.setFileName(filename1);
					}
				}
			}
		}
		return tCostSubPo;

	}

	public TurnoverPo getTurnoverPoByDateAndCustId(FinancingVo financingVo) throws GenericException {
		TurnoverVo turnoverVo = new TurnoverVo();
		turnoverVo.setCustId(financingVo.getCustId());
		turnoverVo.setTurnoverDate(financingVo.getDate());
		List<TurnoverPo> tTurnoverPolist = turnoverDao.getTurnoverPo(turnoverVo);
		if (tTurnoverPolist != null && tTurnoverPolist.size() > 0) {
			return tTurnoverPolist.get(0);

		}
		return null;
	}

	public CostPo getCostPoByDateAndCustId(FinancingVo financingVo) throws GenericException {
		CostVo costVo = new CostVo();
		costVo.setCustId(financingVo.getCustId());
		costVo.setCostDate(financingVo.getDate());
		List<CostPo> tCostlist = costDao.getCostPo(costVo);
		if (tCostlist != null && tCostlist.size() > 0) {
			return tCostlist.get(0);

		}
		return null;
	}

	public void deleteFinancing(FinancingVo financingVo) throws GenericException {
		turnoverDao.deleteTurnover(financingVo);
		costDao.deleteCost(financingVo);
	}

	public String getSelectMaterConcentration(FinancingManagerVo financingManagerVo) throws GenericException {
		List<Object> list = financingDao.getSelectMaterConcentration(financingManagerVo);
		if (list != null && list.size() > 0) {
			Object a = list.get(0);
			if (a != null && !a.equals("")) {
				return a.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public String getSelectMaterSelf(FinancingManagerVo financingManagerVo) throws GenericException {
		List<Object> list = financingDao.getSelectMaterSelf(financingManagerVo);
		if (list != null && list.size() > 0) {
			Object a = list.get(0);
			if (a != null && !a.equals("")) {
				return a.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public String getCostAllValue(FinancingManagerVo financingManagerVo) throws GenericException {
		List<Object> list = financingDao.getCostAllValue(financingManagerVo);
		if (list != null && list.size() > 0) {
			Object a = list.get(0);
			if (a != null && !a.equals("")) {
				return a.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public String getTurnoverAllValue(FinancingManagerVo financingManagerVo) throws GenericException {
		List<Object> list = financingDao.getTurnoverAllValue(financingManagerVo);
		if (list != null && list.size() > 0) {
			Object a = list.get(0);
			if (a != null && !a.equals("")) {
				return a.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public List<Object[]> getUserIdAndUserName(String custId) {
		return financingDao.getUserIdAndUserName(custId);
	}

	public String getCostAllValueGroupByCostType(FinancingManagerVo financingManagerVo) throws GenericException {
		List<Object> list = financingDao.getCostAllValueGroupByCostType(financingManagerVo);
		if (list != null && list.size() > 0) {
			Object a = list.get(0);
			if (a != null && !a.equals("")) {
				return a.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public String getTurnoverAllValueGroupByTurnoverType(FinancingManagerVo financingManagerVo) throws GenericException {
		List<Object> list = financingDao.getTurnoverAllValueGroupByTurnoverType(financingManagerVo);
		if (list != null && list.size() > 0) {
			Object a = list.get(0);
			if (a != null && !a.equals("")) {
				return a.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public void auditFinancing(FinancingVo financingVo) throws GenericException {
		TurnoverVo tTurnoverVo = new TurnoverVo();
		tTurnoverVo.setCustId(financingVo.getCustId());
		tTurnoverVo.setTurnoverDate(financingVo.getDate());
		List<TurnoverPo> tTurnoverPoList = turnoverDao.getTurnoverPo(tTurnoverVo);
		if (tTurnoverPoList != null && tTurnoverPoList.size() > 0) {
			TurnoverPo tTurnoverPo = tTurnoverPoList.get(0);
			tTurnoverPo.setTurnoverState(1);
			turnoverDao.updateTurnoverPo(tTurnoverPo);
		}

		CostVo tCostVo = new CostVo();
		tCostVo.setCustId(financingVo.getCustId());
		tCostVo.setCostDate(financingVo.getDate());
		List<CostPo> tCostPoList = costDao.getCostPo(tCostVo);
		if (tCostPoList != null && tCostPoList.size() > 0) {
			CostPo tCostPo = tCostPoList.get(0);
			tCostPo.setCostState(1);
			costDao.updateCostPo(tCostPo);
		}

	}

	public PaginationSupport findSelectMaterConcentrationDetail(PageController pageController, FinancingDetailVo tFinancingDetailVo)
			throws GenericException {
		return financingDao.findSelectMaterConcentrationDetail(pageController, tFinancingDetailVo);
	}
}
